package com.app.controller.backstage;


import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.StrUtil;
import com.app.common.TotalConfig;
import com.app.model.SysCategory;
import com.app.service.ISysCategoryService;
import com.app.util.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 公用数据配置	 前端控制器
 * </p>
 *
 * @author yoominic
 * @since 2023-12-05
 */
@Api(tags = "admin公用数据配置接口")
@RestController
@RequestMapping("/sysCategory")
public class SysCategoryController {

    @Autowired
    private ISysCategoryService categoryService;


    /**
     * 列表
     *
     * @param code 代码
     * @return {@link Result}
     */
    @ApiOperation(value = "公用数据配置列表")
    @GetMapping("list")
    public Result list(@RequestParam(value = "code") String code) {
        if (!StpUtil.hasPermission("admin:payment:amount:list")){
            return Result.error(TotalConfig.NO_PERMISSION);
        }
        QueryWrapper<SysCategory> sysCategoryQueryWrapper = new QueryWrapper<>();
        sysCategoryQueryWrapper.eq("code", code);
        sysCategoryQueryWrapper.ne("pid", 0);
        return Result.success(this.categoryService.list(sysCategoryQueryWrapper));
    }


    /**
     * 获取公共数据管理树
     *
     * @param keyword 关键字
     * @return {@link Result}
     */
    @ApiOperation(value = "获取公共数据管理树")
    @GetMapping("/getPerTree")
    public Result getPerTree(String keyword) {
        if (!StpUtil.hasPermission("admin:payment:amount:list")){
            return Result.error(TotalConfig.NO_PERMISSION);
        }
        QueryWrapper<SysCategory> queryWrapper = new QueryWrapper<>();
        if (StrUtil.hasBlank(keyword)) {
            queryWrapper.like("name", keyword);
        }
        List<SysCategory> catModelList = categoryService.list(queryWrapper);//全查或者根据name查
        if (catModelList == null) {
            return Result.error("暂无数据");
        }
        List<SysCategory> categoryTree = categoryService.getCategoryTree(catModelList);
        return Result.success(categoryTree);
    }

    /**
     * 添加和更新配置
     *
     * @param sysCategory 系统配置
     * @return {@link Result}
     */
    @ApiOperation(value = "添加和更新配置")
    @PostMapping("/addAndUpdateCategory")
    public Result addAndUpdateCategory(@RequestBody SysCategory sysCategory) {
        if (!StpUtil.hasPermission("admin:system:category:save")) {
            return Result.error(TotalConfig.NO_PERMISSION);
        }
        if (sysCategory.getId() != null && sysCategory.getId() > 0) {
            sysCategory.setUpdateTime(LocalDateTime.now());
            return Result.success(this.categoryService.updateById(sysCategory));
        } else {
            SysCategory category = this.categoryService.getById(sysCategory.getPid());
            sysCategory.setCode(category.getCode());
            sysCategory.setCreateTime(LocalDateTime.now());
            if (sysCategory.getPid() == null) {
                sysCategory.setPid(0);
            }
            return Result.success(this.categoryService.save(sysCategory));
        }
    }

    /**
     * 更新状态
     *
     * @param id     id
     * @param status 状态
     * @return {@link Result}<{@link Object}>
     */
    @ApiOperation(value = "根据公用数据配置表id更新启用禁用状态")
    @GetMapping("/updateCategoryStatus")//修改状态
    public Result updateStatus(@RequestParam("id") Integer id,
                               @RequestParam("status") Integer status) {
        if (!StpUtil.hasPermission("admin:system:category:status")) {
            return Result.error(TotalConfig.NO_PERMISSION);
        }
        SysCategory model = new SysCategory();
        model.setId(id);
        model.setStatus(status);
        categoryService.updateById(model);
        return Result.success("修改成功");
    }

    /**
     * 删除配置
     *
     * @param id id
     * @return {@link Result}<{@link Object}>
     */
    @ApiOperation(value = "删除配置")
    @GetMapping("/deleteCategory")
    public Result deleteCategory(@RequestParam("id") Integer id) {
        if (!StpUtil.hasPermission("admin:system:category:delete")) {
            return Result.error(TotalConfig.NO_PERMISSION);
        }
        SysCategory model = categoryService.getById(id);
        if (model == null) {
            return Result.error("该条数据不存在");
        } else {
            if (model.getPid() == 0) {
                categoryService.removeById(model.getId());
            }
            categoryService.removeById(id);
            return Result.success(null);
        }
    }

    /**
     * 按id获取配置
     *
     * @param id      id
     * @param request 请求
     * @return {@link Result}<{@link Object}>
     */
    @ApiOperation(value = "按id获取配置")
    @GetMapping("/getCategoryById")
    public Result<Object> getCategoryById(Integer id, HttpServletRequest request) {
        if (!StpUtil.hasPermission("admin:system:category:detail")) {
            return Result.error(TotalConfig.NO_PERMISSION);
        }
        SysCategory sysCategory = categoryService.getById(id);
        return Result.success(sysCategory);
    }
}

